package view
{
	import data.DataBase;
	
	import events.GameEvent;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	
	import mapData.MapData;
	
	import utils.Image;
	
	/**
	 * 添加并侦听地块对象 
	 * @author 1
	 * 
	 */	
	public class BuildSelect extends Sprite{
		
		/**
		 * 声明建筑物选择器 
		 */		
		public var buildBar:BuildBar;
		
		/**
		 * 地图数据 
		 * 
		 */
		private var dataBase:DataBase = DataBase.getInstance();
		public function BuildSelect(){
			
			buildInit();
			
		}
		
		/**
		 * 建筑模块初始化 
		 * 
		 */		
		private function buildInit():void{
			//读取关卡数据中的地块位置数组,来添加地图中的地块
//			var mapArr:Array = MapData.BUILD_POSITION;
			var mapArr:Array = dataBase.farm_arr;
			for (var i:int = 0; i < mapArr.length; i++) {
				
				var px:Number = mapArr[i][0];
				var py:Number = mapArr[i][1];
				var landImg:Image = new Image();
				addChild(landImg);
				landImg.source = "assets/landBtn.png";
				landImg.x = px;
				landImg.y = py;
				landImg.useHandCursor = true;
				landImg.buttonMode = true;
				landImg.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandle);
				landImg.addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandle);
				landImg.addEventListener(MouseEvent.CLICK,onLandImgClick);
			}
			
		}
		
		
		/**
		 * 地块被点击 
		 * @param event
		 * 
		 */		
		private function onLandImgClick(event:MouseEvent):void{
			
			var img:Image = event.currentTarget as Image;
			var evt:GameEvent = new GameEvent(GameEvent.BUILDBAR_VIEW,img);
			this.dispatchEvent(evt);
		}
		
		/**
		 * 鼠标离开地块 
		 * @param event
		 * 
		 */		
		private function mouseOutHandle(event:MouseEvent):void{
			var img:Image = event.currentTarget as Image;
			img.filters = null;
		}
		
		/**
		 * 鼠标移动到地块上时 
		 * @param event
		 * 
		 */		
		protected function mouseOverHandle(event:MouseEvent):void{
			var img:Image = event.currentTarget as Image;
			img.filters = [ new GlowFilter( 0xFBF803,1,15,15) ];
		}
		
		
		
	}
}